package com.yinhe.music.yhmusic.login.validcode;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.type.person.BaseInfo;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseActivity;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.network.response.Response;
import com.yinhe.music.yhmusic.network.schedulers.SchedulerProvider;
import com.yinhe.music.yhmusic.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by 何晓华 on 2017/9/25.
 */

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.checkbox)
    AppCompatCheckBox checkbox;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.next)
    Button next;
    private String mUserName;

    BaseModel mModel;
    CompositeDisposable mDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        RxBus.get().register(this);
        ButterKnife.bind(this);
        setToolbar("手机号注册");
        mModel = new BaseModel();
        mDisposable = new CompositeDisposable();
        phone.setFocusable(true);
        phone.requestFocus();
        phone.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        setListener();
    }

    @Override
    protected void onDestroy() {
        mDisposable.dispose();
        super.onDestroy();
    }

    protected void setListener() {
        text.setOnClickListener(view -> startUserAuth());

        next.setOnClickListener(view -> {
            if (!checkbox.isChecked()) {
                Toast.makeText(getApplicationContext(), "同意本协议才可注册", Toast.LENGTH_SHORT).show();
                return;
            }
            getCode();
        });
    }

    private void getCode() {
        mUserName = phone.getText().toString().trim();

        if (TextUtils.isEmpty(mUserName) || mUserName.length() != 11) {
            Toast.makeText(getApplicationContext(), "手机号格式错误", Toast.LENGTH_SHORT).show();
            return;
        }
        Disposable disposable = mModel.getValidCode(IConstants.REGISTER, mUserName)
                .compose(SchedulerProvider.getInstance().applySchedulers())
                .subscribe(baseInfo -> {
                    handleLoginInfo(baseInfo);
                }, throwable -> {
                    ToastUtils.show(throwable.getMessage());
                });
        mDisposable.add(disposable);
    }

    private void handleLoginInfo(Response<BaseInfo> baseInfoResponse) {
        if (baseInfoResponse.getCode() != StatusCode.CHANGE_DATA) {
            Toast.makeText(this, baseInfoResponse.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }
        ToastUtils.show(baseInfoResponse.getMsg());
        Intent intent = new Intent(this, CheckCodeActivity.class);
        intent.putExtra("type", 1);
        intent.putExtra("phone", mUserName);
        startActivity(intent);
    }

    private void startUserAuth() {
        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra("url", IConstants.USER_AUTH_URL);
        intent.putExtra("title", getString(R.string.user_auth_title));
        startActivity(intent);
    }
}
